package cn.ekuma.pos.pda.sale;

import cn.ekuma.android.keyBoardinput.KeyBoradInputPanel;
import cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.BaseBeanListSelecter;
import cn.ekuma.epos.bean.util.sale.TicketHelper;
import cn.ekuma.pos.android.pos.R;
import cn.ekuma.pos.pda.finder.ShardTicketFinder;
import com.openbravo.bean.sales.SharedTicketInfo;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.bean.sales.TicketLine;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class TicketSaleViewActivity extends Activity{

	public static String TICKET_TO_SHOW="ticketToShow";
	
	
	
	protected ImageButton btnSelectShardTicket,btnSelectTable,btnBarcode,btnSelectProduct,btn_adv_newticket,btn_adv_clearticket;
	protected Button btnTicketTitle;
	
	public static final int SELECT_TICKET=0;
	public static final int SELECT_PRODUCTS=1;
	public static final int SELECT_TABLE=2;
	public static final int SELECT_CUSTOMER=3;
	public static final int SCAN_PRODUCTS=4;
	
	protected Button ticket_txt_state;

	protected TicketLines ticketList;
	protected TicketLineListAdapter lineAdapter;
	protected KeyBoradInputPanel keyInportPanel;

	private String shardTicketId;
	
	protected TicketManager ticketManager;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pos_ticket_edit);
		Bundle mBundle=getIntent().getExtras();
		if(mBundle!=null){
		    shardTicketId=(String) mBundle.getSerializable(TICKET_TO_SHOW);
		}
		ticketManager=new TicketManager(this,getHandler());
		lineAdapter=new TicketLineListAdapter(this);
		ticketList=new TicketLines(this,ticketManager,lineAdapter);
		ViewGroup contentPanel=(ViewGroup) this.findViewById(R.id.content_panel);
		contentPanel.addView(ticketList);
		
		ticket_txt_state=(Button) findViewById(R.id.ticket_txt_state);
		
		btnTicketTitle=(Button) findViewById(R.id.btnTicketTitle);
		btnSelectShardTicket=(ImageButton) findViewById(R.id.btnSelectShardTicket);
		btnSelectTable=(ImageButton) findViewById(R.id.btnSelectTable);
		btnSelectProduct=(ImageButton) findViewById(R.id.btnSelectProduct);
		btnBarcode=(ImageButton) findViewById(R.id.btnBarcode);
		
		btn_adv_newticket=(ImageButton) findViewById(R.id.btn_adv_newticket);
		btn_adv_clearticket=(ImageButton) findViewById(R.id.btn_adv_clearticket);
		
		btnSelectShardTicket.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent();
		    	intent.setClass(TicketSaleViewActivity.this, ShardTicketFinder.class);
		    	startActivityForResult(intent,SELECT_TICKET);
			}
			
		});
		keyInportPanel=(KeyBoradInputPanel) findViewById(R.id.ticket_key_import);

		
		
		if(shardTicketId!=null)
			   onLoadData();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
		case SELECT_TICKET:
			SharedTicketInfo selectTicketInfo=(SharedTicketInfo) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
			shardTicketId=selectTicketInfo.getId();
			onLoadData();
			break;
		}
	}

	public void onLoadData() {
		
	}
	
	protected Handler getHandler(){
		return mViewHandler;
	}
	
	
	protected Handler mViewHandler = new Handler() {  
        public void handleMessage (Message msg) {//此方法在ui线程运行   
            switch(msg.what) {  
            case TicketManager.MSG_SUCCESS:  
            	ticketManager.setCurrentTicket((Ticket) msg.obj,true);
            	refushTicket();
                break;  
            case TicketManager.MSG_FAILURE:  
                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
                break;
            }  
        }
    };

    protected void refushTicket(){
    	lineAdapter.clear();
    	Ticket currentTicket=ticketManager.getCurrentTicket();
    	for(TicketLine info:currentTicket.getLines())
    		lineAdapter.add(info);
    	ticketList.getListview().setSelection(currentTicket.getLines().size());
		refushState();
    }
    
    protected void refushState(){
    	btnTicketTitle.setText(TicketHelper.getName(ticketManager.getCurrentTicket()));
    	ticket_txt_state.setText(TicketHelper.printTotal(ticketManager.getCurrentTicket()));
    }
    
    protected void startActivate(Class other){
    	Intent intent = new Intent();
    	intent.setClass(this, other);
    	startActivity(intent);
    }
}